<template>
	<view class="task">
		<view class="headtop">
			<view class="headtitile">
				<image src="../../../static/images/left.png" @click="fanhui"></image>
				<text @click="fanhui">AI拓客</text>
				<view @click="addTask">新建任务</view>
			</view>
		</view>
		<view class="body" v-if="list.length">
			<view class="list" v-for="(item,index) in list" :key="index" @click="detail(item.id)">
				<view class="top clearfix">
					<view class="left fl">ID：{{item.uuid}}</view>
					<view class="right fr" v-if="item.status == 1">进行中</view>
					<view class="jiesu fr" v-if="item.status == -1">已结束</view>
					<view class="wancheng fr" v-if="item.status == 2">已完成</view>
				</view>
				<view class="center clearfix">
					<view class="jindu fl">{{item.doing}}</view>
					<view class="tingzhi fl" v-if="item.status == 1" @click.stop="stop(index)">停止任务</view>
					<view class="del fl" v-else @click.stop="del(index)">删除任务</view>
				</view>
				<view class="bottom">
					任务进度：<text class="bili">{{item.progress}}</text> 成功：<text class="success">{{item.success}}</text>
					失败：<text class="fail">{{item.fail}}</text>
				</view>
			</view>
		</view>
		<view class="empty" v-else>
			<image src="../../../static/images/empty.png" mode="widthFix"></image>
		</view>
	</view>
</template>

<script>
	import {
		request
	} from '@/api/request';

	export default {
		data() {
			return {
				userData: [],
				page: 1,
				list: [],
				addStatus: 0,
				account: 0,
			}
		},
		onShow() {
			this.page = 1;
			this.list = [];

			// 用户基本信息
			request({
				url: '/api/user/profile',
				method: 'GET',
				data: {},
				header: {
					'xx-token': uni.getStorageSync('token')
				}
			}).then((response) => {
				this.userData = response.data;

				this.getList(1);

				// 抖音账号绑定
				this.getAccount();
			}).catch((error) => {});

		},
		onReachBottom() {
			this.page++;
			this.getList(this.page)
		},
		methods: {
			getAccount() {
				request({
					url: '/api/user/cookielist',
					method: 'POST',
					data: {
						user_id: this.userData.id,
					},
				}).then((response) => {
					this.account = response.data.list.length;
				}).catch((error) => {});
			},
			getList(page) {
				let that = this;

				request({
					url: '/api/usergroupbuy/tasklist',
					method: 'POST',
					data: {
						user_id: this.userData.id,
						page: page,
						type: 1,
					},
				}).then((response) => {
					if (response.data.list) {
						let list = response.data.list;

						for (let i = 0; i < list.length; i++) {
							list[i].progress = (list[i].progress * 100).toFixed(2) + '%';

							if (list[i].status == 1) {
								that.addStatus = 1;
							}
						}

						that.list.push(...list)
					}
				}).catch((error) => {});
			},
			stop(index) {
				let that = this;
				let uuid = this.list[index].uuid;

				uni.showModal({
					title: '提示', // 对话框标题
					content: '您确认停止么？', // 显示的内容
					showCancel: true, // 是否显示取消按钮
					success: function(res) {
						if (res.confirm) {

							uni.request({
								url: 'http://127.0.0.1:9999/api/devices/task/stop',
								method: 'POST',
								data: {
									task_id: uuid
								},
							}).then((response) => {
								uni.showToast({
									title: response.data.msg,
									icon: 'none'
								})

								setTimeout(function() {
									that.page = 1;
									that.list = [];
									that.addStatus = 0;

									that.getList(1);
								}, 2000);
							}).catch((error) => {});
						} else if (res.cancel) {

						}
					}
				});
			},
			del(index) {
				let that = this;
				let id = this.list[index].id;

				uni.showModal({
					title: '提示', // 对话框标题
					content: '您确认删除么？', // 显示的内容
					showCancel: true, // 是否显示取消按钮
					success: function(res) {
						if (res.confirm) {
							request({
								url: '/api/usergroupbuy/deletetask',
								method: 'POST',
								data: {
									task_id: id
								},
							}).then((response) => {
								uni.showToast({
									title: response.msg,
									icon: 'none'
								})

								setTimeout(function() {
									that.page = 1;
									that.list = [];
									that.addStatus = 0;

									that.getList(1);
								}, 2000);
							}).catch((error) => {});
						} else if (res.cancel) {

						}
					}
				});
			},
			addTask() {
				if (this.addStatus == 1) {
					uni.showToast({
						title: '请完成任务后,创建新任务',
						icon: 'none'
					})

					return;
				}

				// if(this.account < 5){
				// 	uni.showToast({
				// 		title: '请先绑定5个抖音号',
				// 		icon: 'none'
				// 	})

				// 	return;
				// }

				uni.navigateTo({
					url: '/pages/home/second/taskadd'
				})
			},
			detail(id){
				uni.navigateTo({
					url: '/pages/home/second/task_detail?id=' + id
				})
			},
			fanhui() {
				uni.navigateBack()
			}
		}
	}
</script>

<style scoped lang="scss">
	.task {
		width: 100%;
		margin: 0 auto;

		.headtop {
			width: 100%;
			margin: 0 auto;
			position: relative;
			height: 166rpx;
			z-index: 200;

			.headtitile {
				width: 94%;
				margin: 0 auto;
				position: absolute;
				top: 60rpx;
				left: 3%;
				height: 96rpx;
				line-height: 96rpx;

				image {
					width: 20rpx;
					height: 36rpx;
					float: left;
					margin-right: 18rpx;
					margin-top: 28rpx;
				}

				text {
					font-size: 32rpx;
					color: #FFFFFF;
					float: left;
					height: 96rpx;
					line-height: 96rpx;
				}

				view {
					font-size: 30rpx;
					color: #ECDEA4;
					float: right;
					height: 96rpx;
					line-height: 96rpx;
				}
			}
		}

		.body {
			width: 94%;
			margin: 0 auto 20rpx;

			.list {
				width: 100%;
				margin: 0 auto 20rpx;
				background: #242831;
				box-sizing: border-box;
				border-radius: 10rpx;
				position: relative;
				padding-bottom: 26rpx;

				.top {
					width: 100%;
					margin: 0 auto;
					background: #353a44;
					height: 64rpx;
					line-height: 64rpx;
					border-bottom: 1rpx solid #31353E;
					box-sizing: border-box;
					border-top-right-radius: 10rpx;
					border-top-left-radius: 10rpx;

					.left {
						font-size: 24rpx;
						color: #FFFFFF;
						width: 78%;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
						padding-left: 20rpx;
					}

					.right {
						width: 16%;
						font-size: 30rpx;
						color: #FFA200;
						text-align: right;
						padding-right: 20rpx;
					}

					.jiesu {
						width: 16%;
						font-size: 30rpx;
						color: #00FFF6;
						text-align: right;
						padding-right: 20rpx;
					}

					.wancheng {
						width: 16%;
						font-size: 30rpx;
						color: #2AFF00;
						text-align: right;
						padding-right: 20rpx;
					}
				}

				.center {
					width: 94%;
					margin: 26rpx auto;

					.jindu {
						width: 470rpx;
						height: 60rpx;
						line-height: 60rpx;
						background: #1C1E22;
						border-radius: 10rpx;
						font-size: 24rpx;
						color: #FFFFFF;
						padding-left: 20rpx;
						border: 1rpx solid #454B57;
						margin-right: 20rpx;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}

					.del {
						width: 140rpx;
						text-align: center;
						height: 58rpx;
						line-height: 58rpx;
						border: 2rpx solid #EB1313;
						color: #EB1313;
						font-size: 24rpx;
						border-radius: 5rpx;
					}

					.tingzhi {
						width: 140rpx;
						text-align: center;
						height: 62rpx;
						line-height: 62rpx;
						color: #806B1A;
						font-size: 24rpx;
						background: linear-gradient(270deg, #EAC59F, #E4B985, #FFF9CE);
						border-radius: 5rpx;
					}
				}

				.bottom {
					width: 94%;
					margin: 26rpx auto 0;
					font-size: 24rpx;
					color: #ABB0CC;
					height: 26rpx;
					line-height: 26rpx;

					.bili {
						font-weight: 700;
						color: #2AFF00;
						font-size: 26rpx;
						display: inline-block;
						margin-right: 8rpx;
					}

					.success {
						font-weight: 700;
						color: #0088FF;
						font-size: 26rpx;
						display: inline-block;
						margin-right: 8rpx;
					}

					.fail {
						font-weight: 700;
						color: #EB1313;
						font-size: 26rpx;
					}
				}
			}
		}

		.empty {
			width: 94%;
			margin: 300rpx auto;
			text-align: center;

			image {
				width: 300rpx;
			}
		}
	}
</style>